import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class StudentMan {
    private Connection connection;

    private Map<Integer, testbd> students;

    public StudentMan() throws Exception {
        students = new HashMap<>();

        Properties properties = new Properties();
        properties.load(new FileInputStream("src/druidok.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        connection = dataSource.getConnection();
    }
    //增加学生信息
    public void addstu() throws SQLException {
//        testbd student = new testbd(id, name, age);
//        students.put(id,student);
        String sql="insert into stuhh values (?,?,?)";
        testbd testbd = new testbd();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的id");
        Integer id = Integer.valueOf(scanner.next());
        System.out.println("请输入你的姓名");
        String name = scanner.next();
        System.out.println("请输入你的年龄");
        Integer age = Integer.valueOf(scanner.next());
        testbd.setId(id);
        testbd.setName(name);
        testbd.setAge(age);
//        String sql = "";
        //4.创建数据库操作对象
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,testbd.getId());
        statement.setString(2,testbd.getName());
        statement.setInt(3,testbd.getAge());
        //5.执行sql,获取结果集
        int i = statement.executeUpdate();
        System.out.println("插入 " + i);
        //6.关闭资源
        statement.close();
    }
    //删除学生信息
    public void delstu(int id){
        students.remove(id);
    }
    public void updstu(int id,String name,int age){
        if (students.containsKey(id)){
            testbd student = students.get(id);
            student.setName(name);
            student.setAge(age);
            students.put(id,student);
        }else {
            System.out.println("学生的学号"+id+"不存在");
        }
    }
    //获取学生信息
    public testbd getstu(int id){
        return students.get(id);
    }

    public void print() throws SQLException {
        ArrayList<testbd> testbds = new ArrayList<>();
        String sql = "select *from stuhh";
        PreparedStatement statement = connection.prepareStatement(sql);
        ResultSet resultSet = statement.executeQuery();
        while (resultSet.next()) {
            testbd testbd = new testbd();

            //7.解析结果中数据字段
            testbd.setName(resultSet.getString("name"));
            testbd.setAge(resultSet.getInt("age"));
            testbds.add(testbd);
        }
        for (int i = 0; i < testbds.size(); i++) {
            System.out.println("name = " + testbds.get(i).getName());
            System.out.println("age = " + testbds.get(i).getAge());
        }
        statement.close();
//        for (Student student: students.values()){
//            System.out.println(student);
//        }
    }

    public static void main(String[] args) throws Exception {
        StudentMan studentMan = new StudentMan();
        //添加一些学生信息
        Scanner scanner = new Scanner(System.in);
       studentMan.addstu();
        int choice=0;
        while (choice!=5) {
            System.out.println("菜单");
            System.out.println("1.添加学生信息");
            System.out.println("2.修改学生信息");
            System.out.println("3.删除学生信息");
            System.out.println("4.查看学生信息");
            System.out.println("5.退出");

            choice = scanner.nextInt();
            scanner.nextLine();
            switch (choice) {
                case 1://添加学生信息
//                    System.out.println("请输入学生id");
//                    int id = scanner.nextInt();
//                    System.out.println("请输入学生的姓名");
//                    String name = scanner.next();
//                    System.out.println("请输入学生的年龄");
//                    int age = scanner.nextInt();


                    studentMan.addstu();
                    System.out.println("信息添加成功！");
                    break;
                case 2://修改学生信息
                    System.out.println("请输入要修改的学生id：");
                    int updid = scanner.nextInt();
                    System.out.println("请输入要更新的姓名");
                    String updname = scanner.next();
                    System.out.println("请输入要更新的年龄");
                    int updage = scanner.nextInt();
                    studentMan.updstu(updid, updname, updage);
                    System.out.println("更新成功");
                    break;
                case 3://删除学生信息
                    System.out.println("请输入要删除的学生id");
                    int delid = scanner.nextInt();
                    studentMan.delstu(delid);
                    System.out.println("删除成功");
                    break;
                case 4://查看学生信息
                    System.out.println("所有的学生信息：");
                    studentMan.print();
                    break;
                case 5:
                    System.out.println("退出程序");
                    break;
                default:
                    System.out.println("请输入1-5之间的操作");
                    break;
            }
        }
        scanner.close();
    }
}
